import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:zywz/bloc/home/home_bloc.dart';
import 'package:zywz/bloc/home/home_event.dart';
import 'package:zywz/widgets/financial_card.dart';

class CompanyListPage extends StatefulWidget {
  const CompanyListPage({super.key});

  @override
  State<CompanyListPage> createState() => _CompanyListPageState();
}

class _CompanyListPageState extends State<CompanyListPage> {
  final _formKey = GlobalKey<FormState>();
  final _companyNameController = TextEditingController();
  final _taxCodeController = TextEditingController();
  final _contactPersonController = TextEditingController();
  final _contactPhoneController = TextEditingController();
  String? _selectedProvince;
  String? _selectedCity;

  final List<String> _provinces = ['江苏省', '浙江省', '上海市', '北京市'];
  final List<String> _cities = ['南京市', '苏州市', '无锡市', '常州市'];

  void _submitForm() {
    if (_formKey.currentState!.validate()) {
      final formData = {
        'name': _companyNameController.text,
        'taxCode': _taxCodeController.text,
        'contactPerson': _contactPersonController.text,
        'contactPhone': _contactPhoneController.text,
        'province': _selectedProvince,
        'city': _selectedCity,
      };
      // context.read<HomeBloc>().add(AddCompany(formData));
      Navigator.pop(context);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('添加公司'),
        leading: IconButton(
          icon: const Icon(Icons.arrow_back),
          onPressed: () => Navigator.pop(context),
        ),
      ),
      body: SingleChildScrollView(
        padding: const EdgeInsets.all(16.0),
        child: FinancialCard(
          child: Form(
            key: _formKey,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                const Text('企业基本信息', style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
                const SizedBox(height: 20),

                TextFormField(
                  controller: _companyNameController,
                  decoration: const InputDecoration(
                    labelText: '企业名称',
                    hintText: '请输入企业名称',
                    border: OutlineInputBorder(),
                  ),
                  validator: (value) => value?.isEmpty ?? true ? '请输入企业名称' : null,
                ),
                const SizedBox(height: 16),

                TextFormField(
                  controller: _taxCodeController,
                  decoration: const InputDecoration(
                    labelText: '纳税人识别号',
                    hintText: '请输入纳税人识别号',
                    border: OutlineInputBorder(),
                  ),
                  validator: (value) => value?.isEmpty ?? true ? '请输入纳税人识别号' : null,
                ),
                const SizedBox(height: 16),

                Row(
                  children: [
                    Expanded(
                      child: DropdownButtonFormField<String>(
                        value: _selectedProvince,
                        decoration: const InputDecoration(
                          labelText: '所在省份',
                          border: OutlineInputBorder(),
                        ),
                        items: _provinces.map((province) => DropdownMenuItem(
                          value: province,
                          child: Text(province),
                        )).toList(),
                        onChanged: (value) => setState(() => _selectedProvince = value),
                        validator: (value) => value == null ? '请选择省份' : null,
                      ),
                    ),
                    const SizedBox(width: 16),
                    Expanded(
                      child: DropdownButtonFormField<String>(
                        value: _selectedCity,
                        decoration: const InputDecoration(
                          labelText: '所在城市',
                          border: OutlineInputBorder(),
                        ),
                        items: _cities.map((city) => DropdownMenuItem(
                          value: city,
                          child: Text(city),
                        )).toList(),
                        onChanged: (value) => setState(() => _selectedCity = value),
                        validator: (value) => value == null ? '请选择城市' : null,
                      ),
                    ),
                  ],
                ),
                const SizedBox(height: 16),

                TextFormField(
                  controller: _contactPersonController,
                  decoration: const InputDecoration(
                    labelText: '联系人',
                    hintText: '请输入联系人姓名',
                    border: OutlineInputBorder(),
                  ),
                  validator: (value) => value?.isEmpty ?? true ? '请输入联系人' : null,
                ),
                const SizedBox(height: 16),

                TextFormField(
                  controller: _contactPhoneController,
                  decoration: const InputDecoration(
                    labelText: '联系电话',
                    hintText: '请输入联系电话',
                    border: OutlineInputBorder(),
                  ),
                  keyboardType: TextInputType.phone,
                  validator: (value) => value?.isEmpty ?? true ? '请输入联系电话' : null,
                ),
                const SizedBox(height: 24),

                SizedBox(
                  width: double.infinity,
                  child: ElevatedButton(
                    onPressed: _submitForm,
                    style: ElevatedButton.styleFrom(
                      backgroundColor: const Color(0xFF4DD0E1),
                      padding: const EdgeInsets.symmetric(vertical: 16),
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(8),
                      ),
                    ),
                    child: const Text('提交', style: TextStyle(fontSize: 16, color: Colors.white)),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}